package com.hxtec.polaris.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxtec.polaris.commons.api.vo.Result;
import com.hxtec.polaris.entity.*;
import com.hxtec.polaris.service.CategoryService;
import com.hxtec.polaris.service.ProductBaseService;
import com.hxtec.polaris.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author Caps.Xia
 */
@RestController
@RequestMapping("productManage")
@CrossOrigin(origins = "*",allowCredentials="true",allowedHeaders = "",methods = {})
@Api(value = "/productManage", tags = "商品管理")
public class ProductManageController {

    private final ProductService skuService;
    private final CategoryService categoryService;

    private final ProductBaseService spuService;

    public ProductManageController(ProductService skuService, CategoryService categoryService, ProductBaseService spuService) {
        this.skuService = skuService;
        this.categoryService = categoryService;
        this.spuService = spuService;
    }

    @ApiOperation(value="保存商品信息", notes="商保存商品信息")
    @PostMapping("saveSkuInfo")
    public Result<?> saveSkuInfo(@RequestBody ShopProductVariant skuInfo){
        // 处理默认图片
        String skuDefaultImg = skuInfo.getImage();
        if(StringUtils.isBlank(skuDefaultImg)){
            skuInfo.setImage(skuInfo.getSkuImageList().get(0).getImgUrl());
        }
        skuService.saveSkuInfo(skuInfo);
        return Result.ok("success");
    }

    /**
     * spu销售属性列表
     * @param spuId
     * @return
     */
    @GetMapping("spuSaleAttrList")
    @ApiOperation(value="spu销售属性列表", notes="spu销售属性列表")
    @ResponseBody
    public Result<?> spuSaleAttrList(Integer spuId){

        List<ShopProductSaleAttr> pmsProductSaleAttrs = spuService.spuSaleAttrList(spuId);
        return Result.ok(pmsProductSaleAttrs);
    }

    /**
     * 保存spu信息
     * @param pmsProductInfo
     * @return
     */
    @PostMapping("saveSpuInfo")
    @ApiOperation(value="保存spu信息", notes="保存spu信息")
    @ResponseBody
    public Result<?> saveSpuInfo(@RequestBody ShopProductBase pmsProductInfo){

        spuService.saveSpuInfo(pmsProductInfo);

        return Result.ok("success");
    }


    /**
     * 根据二级分类获取三级分类
     * @param catalog2Id
     * @return
     */
    @ApiOperation("根据二级分类获取三级分类")
    @GetMapping("getCatalog3")
    @ResponseBody
    public Result<?> getCatalog3(String catalog2Id){

        List<ShopCategory> catalog3s = categoryService.getCatalog3(catalog2Id);
        return Result.ok(catalog3s);
    }


    /**
     * 根据一级分类获取二级分类
     * @param catalog1Id
     * @return
     */
    @ApiOperation("根据一级分类获取二级分类")
    @GetMapping("getCatalog2")
    @ResponseBody
    public Result<?> getCatalog2(String catalog1Id){

        List<ShopCategory> catalog2s = categoryService.getCatalog2(catalog1Id);
        return Result.ok(catalog2s);
    }

    /**
     * 获取一级分类
     * @return
     */
    @ApiOperation("获取一级分类")
    @GetMapping("getCatalog1")
    @ResponseBody
    public Result<?> getCatalog1(){

        List<ShopCategory> catalog1s = categoryService.getCatalog1();
        return Result.ok(catalog1s);
    }

    /**
     * 获取spu图片列表
     * @return
     */
    @GetMapping("spuImageList")
    @ApiOperation(value="获取spu图片列表", notes="获取spu图片列表")
    @ResponseBody
    public Result<?> spuImageList(String spuId){
        List<ShopProductImage> productImages = spuService.spuImageList(spuId);
        return Result.ok(productImages);
    }

    /**
     * spu列表信息
     * @return
     */
    @GetMapping("getSpuListInfo")
    @ApiOperation(value="spu列表信息", notes="spu列表信息")
    @ResponseBody
    public Result<?> spuInfoList(Integer currentPage,Integer pageSize,String productName,String productCode,String catalogId){
        PageInfo<ShopProductBase> spuList=spuService.getSpuListInfo(currentPage,pageSize,productName,productCode,catalogId);
        return Result.ok(spuList);
    }

    @GetMapping ("getSkuListBySpuId/{spuId}")
    @ApiOperation(value="获取spu下的sku列表", notes="获取spu下的sku列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "spu", value = "商品的spu", required = true, dataType = "String"),
    })
    public Result<?> getSkuList(@PathVariable int spuId){
        return Result.ok(skuService.getSkuBySkuId(spuId));
    }

}
